package org.jeecg.modules.workflow.controller.change;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boc.ewr.business.entity.SnorgChangeApply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.workflow.vo.change.org.BpmChgSnorgCreateReqVo;
import org.jeecg.common.workflow.vo.change.org.BpmChgSnorgPageReqVo;
import org.jeecg.common.workflow.vo.change.org.BpmChgSnorgRespVo;
import org.jeecg.common.workflow.vo.change.org.BpmChgSnorgUpdateReqVo;
import org.jeecg.common.workflow.vo.oa.BpmOALeaveUpdateReqVO;
import org.jeecg.modules.workflow.controller.BaseController;
import org.jeecg.modules.workflow.convert.change.BpmChgSnorgConvert;
import org.jeecg.modules.workflow.service.change.BpmChgSnorgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author : Wzz
 * @create : 2023-04- 26- 14:30
 * @desc :
 */
@Api(tags = "管理后台 - 签约机构变更申请")
@RestController
@RequestMapping("/bpm/conage/leave")
@Validated
public class BpmChgSnorgController extends BaseController {

    @Resource
    private BpmChgSnorgService service;

    @Autowired
    private BpmChgSnorgConvert convert;

    @PostMapping("/create")
    //@RequiresPermissions("bpm:conage:create")
    @ApiOperation("创建签约机构变更申请")
    public Result<Long> createLeave(@Valid @RequestBody BpmChgSnorgCreateReqVo createReqVO) {
        return Result.OK(service.createChgSnorg(getUserId(), createReqVO));
    }

    @GetMapping("/get")
    //@RequiresPermissions("bpm:conage:query")
    @ApiOperation("获得签约机构变更申请")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public Result<BpmChgSnorgRespVo> getLeave(@RequestParam("id") String id) {
        SnorgChangeApply chgSnorg = service.getChgSnorg(id);
        return Result.OK(convert.convert(chgSnorg));
    }

    @GetMapping("/page")
    //@RequiresPermissions("bpm:conage:query")
    @ApiOperation("获得签约机构变更申请分页")
    public Result<IPage<BpmChgSnorgRespVo>> getLeavePage(@Valid BpmChgSnorgPageReqVo pageVO) {
        IPage<SnorgChangeApply> chgSnorgPage = service.getChgSnorgPage(getUserId(), pageVO);
        IPage<BpmChgSnorgRespVo> bpmChgSnorgRespVoIPage = convert.convertPage(chgSnorgPage);
        return Result.OK(bpmChgSnorgRespVoIPage);
    }

    @ApiOperation(value="签约机构变更申请-通过协议号获取信息", notes="签约机构变更申请-通过协议号获取信息")
    //@RequiresPermissions("bpm:conage:getInfo")
    @GetMapping(value = "/getInfo/{protocolNo}")
    public Result<BpmChgSnorgRespVo> getInfoByProtocolNo(@PathVariable String protocolNo) {
        BpmChgSnorgRespVo bpmChgSnorgRespVo = service.getInfoByProtocolNo(protocolNo);

        return Result.ok(bpmChgSnorgRespVo);
    }

    @PutMapping("/update")
    //@PreAuthorize("@ss.hasPermi('bpm:conage:update')")
    @ApiOperation("更新签约机构变更申请")
    public Result<Boolean> updateLeave(@Valid @RequestBody BpmChgSnorgUpdateReqVo updateReqVO) {
        service.updateChgSnorg(getUserId(), updateReqVO);
        return Result.OK(true);
    }



    private String getUserId()
    {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser.getId();
    }
}
